﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace CodeJumper
{
    public sealed class ElementStackPanel : Border
    {
        private readonly ApplicationConfiguration _appConfig;
        private readonly StackPanel _childrenStackPanel = new StackPanel();
        private readonly StackPanel _headerStackPanel = new StackPanel();
        private readonly StackPanel _unifyingStackPanel = new StackPanel();
        private bool _showBorder = true;
        private Expander expander;

        public ElementStackPanel(ApplicationConfiguration appConfig)
        {
            _appConfig = appConfig;
            _childrenStackPanel.CanVerticallyScroll = true;
            _childrenStackPanel.HorizontalAlignment = HorizontalAlignment.Stretch;
            _childrenStackPanel.VerticalAlignment = VerticalAlignment.Stretch;

            _unifyingStackPanel.Children.Add(_headerStackPanel);
            _unifyingStackPanel.Children.Add(_childrenStackPanel);

            Child = _unifyingStackPanel;

            CornerRadius = appConfig.StackableBorder.CornerRadius;
            BorderBrush = appConfig.StackableBorder.BorderBrush;
            BorderThickness = appConfig.StackableBorder.BorderThickness;
            Padding = appConfig.StackableBorder.Padding;
        }

        public bool ShowBorder
        {
            get { return _showBorder; }
            set
            {
                _showBorder = value;
                if (_showBorder)
                    BorderBrush = _appConfig.StackableBorder.BorderBrush;
                else
                    BorderBrush = Brushes.Transparent;
            }
        }

        public UIElementCollection Children
        {
            get { return _childrenStackPanel.Children; }
        }

        public Orientation Orientation
        {
            get { return _childrenStackPanel.Orientation; }
            set { _childrenStackPanel.Orientation = value; }
        }

        public void SetHeader(UIElement element)
        {
            _headerStackPanel.Children.Clear();
            _headerStackPanel.Children.Add(element);
        }

        public void Collapse()
        {
            _childrenStackPanel.Visibility = Visibility.Collapsed;
            expander.IsExpanded = false;
        }

        public void Expand()
        {
            _childrenStackPanel.Visibility = Visibility.Visible;
            expander.IsExpanded = true;
        }

        private void StartStoryBoardShrink()
        {
            var storyboard = new Storyboard();

            var scale = new ScaleTransform(1.0, 1.0);
            _childrenStackPanel.RenderTransform = scale;

            var growAnimation = new DoubleAnimation();
            growAnimation.Duration = TimeSpan.FromMilliseconds(300);
            growAnimation.From = 1;
            growAnimation.To = 0;

            var sizeAnimation = new DoubleAnimation(0, TimeSpan.FromMilliseconds(300));

            storyboard.Children.Add(growAnimation);

            Storyboard.SetTargetProperty(growAnimation, new PropertyPath("RenderTransform.ScaleY"));
            Storyboard.SetTargetProperty(sizeAnimation, new PropertyPath("Height"));

            Storyboard.SetTarget(growAnimation, _childrenStackPanel);
            storyboard.Completed += (s, e) => { _childrenStackPanel.Visibility = Visibility.Collapsed; };


            storyboard.Begin();
        }

        internal void SetExpander(Expander expander)
        {
            this.expander = expander;
        }
    }
}